package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.ticket.domain.*;
import cn.wolfcode.luowowo.ticket.service.IDestService;
import cn.wolfcode.luowowo.ticket.service.ITicketOrderService;
import cn.wolfcode.luowowo.ticket.service.ITicketSaleService;
import cn.wolfcode.luowowo.ticket.service.ITicketService;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.UUID;

/**
 * 主页控制
 */
@Controller

public class TicketController {

    @Reference
    private ITicketService ticketService;
    @Reference
    private IDestService destService;
    @Reference
    private ITicketSaleService ticketSaleService;
    @Reference
    private ITicketOrderService ticketOrderService;
    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;

    /**
     * 发送验证码
     *
     * @param phone
     * @return
     */
    @RequestMapping("/ticket/sendVerifyCode")
    @ResponseBody
    public AjaxResult sendVerifyCode(String phone) {
        AjaxResult result = new AjaxResult();
        //获取验证码
        String verifyCode4Redis = verifyCodeCacheService.getVerifyCode4Ticket(phone);
        //判断是否已经发送过验证码
        if (verifyCode4Redis != null) {
            return result.mark("请不要在60s内重复发送验证码");
        }
        //生成验证码
        String verifyCode = UUID.randomUUID().toString().substring(0, 4);
        System.out.println(phone + "的购票验证码为:" + verifyCode);
        try {
            //存入redis
            verifyCodeCacheService.setVerifyCode4Ticket(phone, verifyCode);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;

    }

    /**
     * 效验购票验证码和手机
     *
     * @param phone
     * @param verifyCode
     * @return
     */
    @RequestMapping("/ticket/checkPhoneAndCode")
    @ResponseBody
    public AjaxResult checkPhoneAndCode(String phone, String verifyCode) {
        return ticketService.checkPhoneAndCode(phone, verifyCode);

    }


    @RequestMapping("/ticket")
    public String index1(Model model) {
        //查询4个门票信息
        List<Ticket> tickets = ticketService.listTicketTop4();
        model.addAttribute("tickets", tickets);

        //查询所有地点
        List<Dest> dests = destService.selectAll();
        model.addAttribute("dests", dests);
        return "/ticket/index";
    }

    /**
     * 根据目的地和标签找到对应的地点
     *
     * @param ajaxDestId
     * @param ajaxSubjectId
     * @return
     */
    @RequestMapping("homePage/reloadProd")
    public String themes(Model model, Long ajaxDestId, Long ajaxSubjectId) {

        //tickets
        List<Ticket> tickets = ticketService.selectTicksByDestIdAndSubjectId(ajaxDestId, ajaxSubjectId);

        model.addAttribute("tickets", tickets);

        return "ticket/reloadProd";
    }

    /**
     * 找出所有目的地
     *
     * @return
     */
    @RequestMapping("/homePage/getDestination")
    public String dests(Model model) {
        //dests
        List<Dest> dests = destService.selectAll();
        model.addAttribute("dests", dests);
        return "ticket/dest";
    }

    /**
     * 根据目的地找到标签
     *
     * @param ajaxDestId
     * @return
     */
    @RequestMapping("homePage/reloadSubAndProd")
    public String themes(Model model, Long ajaxDestId) {

        //找出目的地下的所有主题
        List<Subject> subjects = ticketService.selectSubjectsByDestId(ajaxDestId);
        model.addAttribute("themes", subjects);

        //sight   查询出来做缓存使用   先获取本目的地下的第一个标签
        List<Long> subjectIds = destService.getSubjectIdsByDestId(ajaxDestId);

        if (subjectIds.size() != 0) {
            List<Ticket> sights = ticketService.selectTicksByDestIdAndSubjectId(ajaxDestId, subjectIds.get(0));
            model.addAttribute("sights", sights);
        }
        return "ticket/reloadSubAndProd";
    }

    /**
     * 门票详情页面
     *
     * @param model
     * @param tid
     * @return
     */
    @RequestMapping("ticket/detail")
    public String detail(Model model, Long tid) {
        //景点
        Ticket ticket = ticketService.selectById(tid);
        model.addAttribute("ticket", ticket);

        //景点售票
        List<TicketSale> sales = ticketSaleService.selectSaleByTicketId(tid);
        model.addAttribute("sales", sales);

        //景点对应的标签
        Subject subject = ticketService.selectSubjectByTicketId(tid);
        model.addAttribute("subject", subject);

        //景点对应的目的地
        Dest dest = ticketService.selectDestByTicketId(tid);
        model.addAttribute("dest", dest);
        //commends  相关景点推荐  前三个相同目的地景点
        List<Ticket> commends = ticketService.selectTicketTop5ByDestId(ticket.getAjaxDestId());
        model.addAttribute("commends", commends);
        //相同标签景点推荐 tickets
        List<Ticket> tickets = ticketService.selectTicketTop5BySubjectId(ticket.getAjaxSubjectId());
        model.addAttribute("tickets", tickets);
        /*if(tid != null) {



            //sales 详细门票信息
            List<TicketSale> sales = ticketSaleService.selectByTid(tid);

            model.addAttribute("sales",sales);

        }*/

        return "ticket/detail";
    }

    /**
     * 购票页面
     *
     * @param tid
     * @param model
     * @return
     */
    @RequiredLogin
    @RequestMapping("/ticket/addOrder")
    public String order(Long tid, Long id, Model model) {
        TicketSale sale = ticketSaleService.selectSaleByIdAndTicketId(id, tid);
        model.addAttribute("sale", sale);
        /*Ticket ticketDetail = ticketService.selectById(tid);
        model.addAttribute("ticketDetail",ticketDetail);*/
        return "ticket/addOrder";
    }

    @RequestMapping("/ticket/submitOrder")
    @ResponseBody
    public Object submitOrder(TicketOrder ticketOrder, Model model) {
        AjaxResult result = new AjaxResult();
        try {
            ticketOrderService.save(ticketOrder);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }


    /**
     * 获取地理位置
     *
     * @param id    景点id
     * @param model 模型对象
     * @return 地图页面
     */
    @RequestMapping("/ticket/map")
    public String map(Long id, Model model) {
        //默认值
        String lng = "116.413554";
        String lat = "39.911013";
        //通过id获取坐标
        Ticket ticket = ticketService.selectCoordinateById(id);
        if (ticket != null && ticket.getLng() != null) {
            lng = ticket.getLng();
        }
        if (ticket != null && ticket.getLat() != null) {
            lat = ticket.getLat();
        }
        model.addAttribute("lng", lng);
        model.addAttribute("lat", lat);
        return "/ticket/map";
    }
}
